import Vue from 'vue'
import Vuex from 'vuex'
import { UserInfo } from '../types/user'
import integral from './modules/integral'
import user from './user'

Vue.use(Vuex)

export interface RootState {
  user: UserInfo | null
  userToken:  any | null
  [key: string]: any
}

export default new Vuex.Store<RootState>({
  state: {
    user: null,
	userToken:null,
	is_gray_scale: null,
	orderDetail: null
  },
  mutations: {
    setUser(state, user: UserInfo) {
      state.user = user
    },
	setData(state, data:any) {
	  state[data['_key']] = data['_val']
	}
  },
  actions: {
    async fetchUserInfo({ commit }) {
		const user: UserInfo = {
			avatar: "https://randomuser.me/api/portraits/women/44.jpg",
			coupons: 3,
			id: 1,
			integral: 200,
			level: 1,
			location: "红山区",
			name: "赵大美",
			points: 0,
		}
      // Implement your user info fetching logic here
      // const user = await api.getUserInfo()
	  uni.setStorageSync('_UserInfo', user)
      commit('setUser', user)
    },
	async setStoreData({ commit }, dd:any) {
		commit('setData', dd)
	}
  },
  modules: {
    integral,
    user
  }
}) 